<?php
/* @var $this MenuTestController */
/* @var $model Menuadmin */

$this->breadcrumbs=array(
	'Quản lý menu',
);

$this->menu=array(
	array('label'=>'Thêm menu', 'url'=>array('add')),
);
?>

<div class="wap-search">      
   <div class="form-search" id="<?php echo Yii::app()->controller->id . '-' . Yii::app()->controller->action->id?>">
      <?php $form=$this->beginWidget('CActiveForm', array(
      	'id'=>Yii::app()->controller->id . '_' . Yii::app()->controller->action->id,
      	'enableAjaxValidation'=>false,
         'enableClientValidation'=>true
      )); ?>
      
      
      	<?php echo $form->errorSummary($model,'Kiểm tra lại các giá trị bạn vừa nhập vào'); ?>
         <input name="session_user" type="hidden" value="<?php echo $session_user?>"/>
         <input name="action_user" type="hidden" value="search" id="action_user" />
            <table>
               <tr>
                  <td><?php echo $form->textField($model, 'MenuName',array('value' => "",'placeholder' => 'Tên menu','style' => 'width:150px;padding:0.5px 3px;'));?></td>
                  <td><?php echo $form->dropDownList($model, 'Menu1',$menu_level_1);?></td>
                  <td><?php echo $form->dropDownList($model, 'Menu2',$menu_level_2);?></td>
                  <td>
                  <?php $model->IsActive = (is_null($model->IsActive)) ? 2 : $model->IsActive ?>
                  <?php echo $form->dropDownList($model,"IsActive",array("2" => 'Tất cả','1' => 'Kích hoạt','0' => 'Chưa kích hoạt'))?>
                  </td>
                  <td><input class="btn" type="submit" value="Tìm kiếm" /></td>
               </tr>
            </table>
      <?php $this->endWidget(); ?>
   </div> 
</div>
<div class="list-menu" id="grid">
   <table>
      <tr class="GridHeader">
         <th style="width:18px;" class=""><input class="row-check-all" type="checkbox" id="chk_SelectAllRow"></th>
         <th style="width:36px;"><div>STT</div></th>
         <th><div lang="MenuName"><span class="GridColumnSort">Tên menu</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="MenuPath"><span class="GridColumnSort">Đường dẫn</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="MenuCode"><span class="GridColumnSort">Mã menu</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="MenuIcon"><span class="GridColumnSort">Icon</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="MenuImage"><span class="GridColumnSort">Ảnh đại diện</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="Menu1"><span class="GridColumnSort">Mức 1</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="Menu2"><span class="GridColumnSort">Mức 2</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="Menu3"><span class="GridColumnSort">Mức 3</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="Menu4"><span class="GridColumnSort">Mức 4</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="IsHome"><span class="GridColumnSort">Trang chủ</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
         <th><div lang="IsActive"><span class="GridColumnSort">Trạng thái</span><div class="GridShortMain"><div class="GridShort"></div></div></div></th>
     </tr>
     <?if(count($list) > 0){
      $i = 0;
      foreach($list as $item){
         $i++;
         ?>
         <tr class="grid-row-<?php echo $i%2?>">
            <td class="select-row">
               <input class="row-check" type="checkbox" value="<?php echo $item['MenuAdminID']?>"/>
            </td>
            <td align="center"><?php echo $i?></td>
            <td><?php echo $item['MenuName']?></td>
            <td><?php echo $item['MenuPath']?></td>
            <td><?php echo $item['MenuCode']?></td>
            <td style="text-align: center;"></td>
            <td style="text-align: center;">
            <img style="width: 50px;" class="ShowPopupImage" url="<?php echo Yii::app()->request->baseUrl . '/images/menu/' . $item['MenuImage']?>" alt="" src="<?php echo Yii::app()->request->baseUrl . '/images/menu/' . $item['MenuImage']?>" onerror="this.src='<?php echo Yii::app()->request->baseUrl .'/images/Document.png'?>'" />
            </td>
            <td align="center"><?php echo $item['Menu1']?></td>
            <td align="center"><?php echo $item['Menu2']?></td>
            <td align="center"><?php echo $item['Menu3']?></td>
            <td align="center"><?php echo $item['Menu4']?></td>
            <td align="center"><?php echo ($item['IsHome'] == 1) ? 'Có' : 'Không' ?></td>
            <td align="center"><?php echo ($item['IsActive'] == 1) ? 'Kích hoạt' : 'Chưa kích hoạt' ?></td>
       </tr>
      <?}
     }?>
    
   </table>
   <form class="form" id="option_form" action="" method="POST">
      <table>
         <tr>
            <td>  
               Số dòng:
               <select name="limit" id="limit">
                  <?foreach($rowList as $value){?>
                     <option value="<?php echo $value?>" <?php if($value == $limit) echo 'selected'?> > <?php echo $value?></option>
                  <?}?>
               </select>
            </td>
            <td class="text-center">
               Trang:
               <input id="page_input" name="page" type="text" value="<?php echo $page?>" />
               Tổng số:
               <?php echo $total_pages?>
            </td>
            <td class="text-right">
               Xem:
                  <?php echo $start + 1?>
                   - 
                  <?php 
                  $end = $limit*$page;
                  $end = ($end > $total_record) ? $total_record : $end;
                  echo $end ?>
               Tổng số:
                  <?php echo $total_record?>
            </td>
         </tr>
      </table>
   </form>
</div>
<script>
   $(document).ready(function(){
      //Select all record
      $("#chk_SelectAllRow").click(function(){
         var checkboxes = $(".row-check");
         if($(this).is(':checked')) {
            checkboxes.attr('checked', 'checked');
         } else {
            checkboxes.removeAttr('checked');
         }
      });
      
      //Submit form when limit change
      $("#limit").change(function(){
         $("#option_form").submit();
      });
      
      //Submit form when page change
      var old_page;
      var new_page;
      $("#page_input").focus(function(){
         old_page = $(this).val();
      });
      $("#page_input").blur(function(){
         new_page = $(this).val();
         if(new_page !== old_page) $("#option_form").submit();
      });
      
      //Deactive record
      $("#deactive").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('deactive')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Cập nhật thành công " + data.record_affected + " bản ghi.");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Active record
      $("#active").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('active')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Cập nhật thành công " + data.record_affected + " bản ghi.");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Delete record
      $("#delete").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('delete')?>';
            var succ = function(data){
               
               if(data.error == 0){
                  alert("Có " + data.record_affected + " bản ghi bị xóa");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Edit record 
      var id_edit = 0;
      $(".row-check").click(function(){
         id_edit = $(this).val();
      });
      $("#edit").click(function(){
         if(id_edit == 0){
            alert("Bạn phải chọn 1 bản ghi để sửa")
         }else{
            window.location.href = '<?php echo CController::createUrl('edit')?>' + '/' + id_edit;
         }
      });
         
   });
</script>
<script>
   $(document).ready(function(){
      //Load menu level 2 when selected menu level 1
      $("#Menuadmin_Menu1").change(function(){
         var url = '<?php echo CController::createUrl('loadMenu2Index')?>';
         var menu = $('option:selected',this).val();
         data = {"menu":menu};
         var succ = function(data){
            $("#Menuadmin_Menu3").html('<option value="0" menu="0">-- Nhóm 3 --</option>');
            $("#Menuadmin_Menu2").html(data);
         };
         handleAjax(url,'POST','',data,succ);
      });
      
      
   });
</script>
